package com.hollycrm.unicom.sheet.business.logic.hibernate;

import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.domain.ConfirmSheet;
import com.hollycrm.unicom.sheet.domain.DealSheet;
import com.hollycrm.unicom.sheet.domain.MainSheet;
import com.hollycrm.unicom.sheet.domain.SheetRoutineNote;
import com.hollycrm.unicom.sheet.flow.SheetAction;
import com.hollycrm.unicom.sheet.flowsupport.ActionExecuteLogic;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

/**
 * HibernateAddRoutineNoteLogic
 *
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Tian Kai
 * @version 1.0
 */
public class HibernateAddRoutineNoteLogic extends HibernateDaoSupport
    implements ActionExecuteLogic {
    /**
     * execute
     *
     * @param authToken AuthToken
     * @param action SheetAction
     * @param context DoOperateContext
     * @throws Exception
     */
    public void execute(AuthToken authToken, SheetAction action, DoOperateContext context) throws
        Exception {
        HibernateTemplate ht = getHibernateTemplate();
        SheetRoutineNote routineNote = (SheetRoutineNote) context.getInputAttr(
            SheetRoutineNote.class);
        ht.save(routineNote);
        if (Constants.SHEET_TYPE_MAIN.equals(routineNote.getSheetType()))
            noteForMainSheet(routineNote, context);
        else if (Constants.SHEET_TYPE_DEAL.equals(routineNote.getSheetType()))
                 noteForDealSheet(routineNote, context);
        else if (Constants.SHEET_TYPE_CONFIRM.equals(routineNote.getSheetType()))
                 noteForConfirmSheet(routineNote, context);
        else
            logger.warn("[w] illegal sheetType: " + routineNote.getSheetType());
    }

    /**
     * noteForConfirmSheet
     *
     * @param routineNote SheetRoutineNote
     * @param context DoOperateContext
     * @throws Exception
     */
    private void noteForConfirmSheet(SheetRoutineNote routineNote, DoOperateContext context) throws Exception {
        HibernateTemplate ht = getHibernateTemplate();
        ConfirmSheet confirmSheet = (ConfirmSheet) ht.load(ConfirmSheet.class,
            routineNote.getSheetFlowNO());
        confirmSheet.increaseNoteCount();
        confirmSheet.getRoutineNoteDms().add(routineNote);
        context.addOutputAttr(confirmSheet.getMainSheet());
    }

    /**
     * noteForDealSheet
     *
     * @param routineNote SheetRoutineNote
     * @param context DoOperateContext
     * @throws Exception
     */
    private void noteForDealSheet(SheetRoutineNote routineNote, DoOperateContext context) throws Exception {
        HibernateTemplate ht = getHibernateTemplate();
        DealSheet dealSheet = (DealSheet) ht.load(DealSheet.class,
                                                  routineNote.getSheetFlowNO());
        dealSheet.increaseNoteCount();
        dealSheet.getRoutineNoteDms().add(routineNote);
        context.addOutputAttr(dealSheet.getMainSheet());
    }

    /**
     * noteForMainSheet
     *
     * @param routineNote SheetRoutineNote
     * @param context DoOperateContext
     * @throws Exception
     */
    private void noteForMainSheet(SheetRoutineNote routineNote, DoOperateContext context) throws Exception {
        HibernateTemplate ht = getHibernateTemplate();
        MainSheet mainSheet = (MainSheet) ht.load(MainSheet.class,
                                                  routineNote.getSheetFlowNO());
        mainSheet.increaseNoteCount();
        mainSheet.getRoutineNoteDms().add(routineNote);
        mainSheet.setModifyAgentCode(routineNote.getAgentCode());
        mainSheet.setModifyTime(routineNote.getEndTime());
        context.addOutputAttr(mainSheet);
    }

}
